package com.tannm.doan.taskmanager.reminder;

import com.tannm.doan.taskmanager.R;
import java.util.ArrayList;
import java.util.Calendar;

import com.tannm.doan.taskmanager.*;
import com.tannm.doan.taskmanager.views.*;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import android.app.NotificationManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.os.Bundle;

public class AlarmReminder {
	// fields
	private Context context = null;
	protected AlarmService.AlarmPrefs prefs = null;
	protected Utils utils = null;
	private ArrayList<AlarmDataViewItem> alarmItems = new ArrayList<AlarmDataViewItem>();
	private NotificationManager mNM = null;
	private final int iNotifyID1 = 1;
	private int iLastAlarmsHash = 0;
	private Calendar calHashToday = Calendar.getInstance();
	// private AlarmSound alarmSound = null;

	// fields
	private String msgYouHaveASTS = "";
	private String msgYouHaveTS = "";
	private String msgYouHaveAS = "";
	private String msgASTS = "";
	private String msgTS = "";
	private String msgAS = "";
	private String strReminder = "";

	// fields
	private String sAppName = "";

	// methods
	public AlarmReminder(Context ctx, AlarmService.AlarmPrefs prefs, Utils utils) {
		this.context = ctx;
		this.prefs = prefs;
		this.utils = utils;

		sAppName = this.context.getString(R.string.app_name);
		mNM = (NotificationManager) ctx
				.getSystemService(Context.NOTIFICATION_SERVICE);
		removeNotify();

		// localize strings
		msgYouHaveASTS = utils.getResStr(R.string.msgYouHaveASTS);
		msgYouHaveTS = utils.getResStr(R.string.msgYouHaveTS);
		msgYouHaveAS = utils.getResStr(R.string.msgYouHaveAS);
		msgASTS = utils.getResStr(R.string.msgASTS);
		msgTS = utils.getResStr(R.string.msgTS);
		msgAS = utils.getResStr(R.string.msgAS);
		strReminder = utils.getResStr(R.string.strReminder);

		// get alarm sound
		// alarmSound = new AlarmSound(context);
	}

	public void clear() {
		alarmItems.clear();
	}

	public void removeNotify() {
		mNM.cancel(iNotifyID1);
	}

	public void addAlarmData(ArrayList<AlarmDataViewItem> items) {
		this.alarmItems.addAll(items);
	}

	public boolean isAlarm() {
		return (alarmItems.size() > 0);
	}

	public int getItemsCount(int iOrderFilter) {
		int iCount = 0;
		for (int index = 0; index < alarmItems.size(); index++) {
			AlarmDataViewItem item = alarmItems.get(index);
			if (item.getOrder() == iOrderFilter)
				iCount++;
		}
		return iCount;
	}

	public String getPopupMsg(int iCountAppts, int iCountTasks) {
		String msg = String.format(msgYouHaveASTS, iCountAppts, iCountTasks);
		if ((iCountAppts == 0) && (iCountTasks > 0))
			msg = String.format(msgYouHaveTS, iCountTasks);
		if ((iCountTasks == 0) && (iCountAppts > 0))
			msg = String.format(msgYouHaveAS, iCountAppts);
		return msg;
	}

	public String getStatusMsg(int iCountAppts, int iCountTasks) {
		String msg = String.format(msgASTS, iCountAppts, iCountTasks);
		if ((iCountAppts == 0) && (iCountTasks > 0))
			msg = String.format(msgTS, iCountTasks);
		if ((iCountTasks == 0) && (iCountAppts > 0))
			msg = String.format(msgAS, iCountAppts);
		return msg;
	}

	public String getTodayHashString() {
		calHashToday.setTimeInMillis(System.currentTimeMillis());
		return Integer.toString(calHashToday.get(Calendar.YEAR))
				+ Integer.toString(calHashToday.get(Calendar.MONTH))
				+ Integer.toString(calHashToday.get(Calendar.DAY_OF_MONTH));
	}

	public String getAlarmsHashString() {
		String sMsg = "";
		for (int index = 0; index < alarmItems.size(); index++) {
			AlarmDataViewItem item = alarmItems.get(index);
			sMsg += item.getHashString();
		}
		// add today hash, for changing hash for next day compare (repeat
		// appointments)
		sMsg += getTodayHashString();
		return sMsg;
	}

	public boolean alarmsChanged() {
		String sAlarmsHashString = getAlarmsHashString();
		if (sAlarmsHashString.hashCode() == iLastAlarmsHash)
			return false;
		iLastAlarmsHash = sAlarmsHashString.hashCode();
		return true;
	}

	public void putDataItemsToBundle(Bundle data, int iOrderFilter) {
		int iKeyIndex = 0;
		for (int i = 0; i < alarmItems.size(); i++) {
			AlarmDataViewItem item = alarmItems.get(i);
			if (item.getOrder() == iOrderFilter) {
				String sKey = "_" + Integer.toString(iOrderFilter) + "_"
						+ Integer.toString(iKeyIndex);

				data.putLong("rowid" + sKey, item.getID());
				data.putString("text" + sKey, item.getText(utils,
						prefs.b24HourMode));

				iKeyIndex++;
			}
		}
		data.putInt("typetotal_" + Integer.toString(iOrderFilter), iKeyIndex);
	}

	public PendingIntent getAlarmDialogIntent() {
		Intent intentAlarmDialog = new Intent(
				"android.intent.action.TaskManager.ACTION_MODE_VIEW_ALARMDIALOG");
		Bundle data = new Bundle();
		putDataItemsToBundle(data, AlarmDataViewItem.iOrderAppts);
		putDataItemsToBundle(data, AlarmDataViewItem.iOrderTasks);
		intentAlarmDialog.putExtras(data);
		// return PendingIntent.getActivity(this.context, 0, intentAlarmDialog,
		// PendingIntent.FLAG_CANCEL_CURRENT);
		return PendingIntent.getActivity(this.context, 0, intentAlarmDialog, 0);
	}

	public void showNotification() {
		if (isAlarm()) {
			int iCountAppts = getItemsCount(AlarmDataViewItem.iOrderAppts);
			int iCountTasks = getItemsCount(AlarmDataViewItem.iOrderTasks);

			// is there something to show ?
			if ((iCountAppts > 0) || (iCountTasks > 0)) {
				// check if alarms changed from last time
				if (alarmsChanged()) {
					// remove last notify from status bar
					removeNotify();

					// make notify text
					final String sTitle = sAppName + ": " + strReminder;
					String sMessage = getPopupMsg(iCountAppts, iCountTasks);

					// make notify status
					String sStatus = getStatusMsg(iCountAppts, iCountTasks);

					// show popup message
					ViewToastMsg viewToastMsg = new ViewToastMsg(this.context,
							sTitle, sMessage);
					Toast toast = new Toast(this.context);
					toast.setDuration(Toast.LENGTH_LONG);
					toast.setView(viewToastMsg);
					toast.show();

					// show status bar notification item
					Notification notifyItem = new Notification(
							R.drawable.stat_notify_alarm, sStatus, System
									.currentTimeMillis());

					/*
					 * final Uri uriAlarmSound = alarmSound.getUri(); if
					 * (uriAlarmSound == null) { notifyItem.defaults =
					 * Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS;
					 * } else { notifyItem.defaults =
					 * Notification.DEFAULT_LIGHTS; notifyItem.sound =
					 * uriAlarmSound; notifyItem.audioStreamType =
					 * AudioManager.STREAM_ALARM; }
					 */

					notifyItem.defaults = Notification.DEFAULT_SOUND
							| Notification.DEFAULT_LIGHTS;
					notifyItem.setLatestEventInfo(this.context, strReminder,
							sStatus, getAlarmDialogIntent());

					// run
					mNM.notify(iNotifyID1, notifyItem);
				}
			}
		} else {
			removeNotify();
			iLastAlarmsHash = 0;
		}
	}

}
